<?php

class Test_Abovo_Model_Tag extends Solar_Test {
    
    public function setup()
    {
        $this->_tag = Solar::factory('Abovo_Model_Tag', array('area' => 'test'));
    }
    
    public function test__construct()
    {
        try {
            Solar::factory('Abovo_Model_Tag');
        } catch(Solar_Exception $e) {
            $this->assertInstance($e, 'Solar_Exception');
            return;
        }
        
        $this->fail();
    }
    
    public function testRefresh()
    {
        Solar::registry('sql')->query('DELETE from tagmap');
        
        $tags = 'tag, tag1, tag2';
        $this->_tag->refresh($tags, 1, 'antti');

        $tags = 'tag3';
        $this->_tag->refresh($tags, 1, 'antti');

        $tags = array('tag4', 'tag5', 'tag6');
        $this->_tag->refresh($tags, 2, 'antti');

        $tags = 'tag7, tag8, tag9';
        $this->_tag->refresh($tags, 1, 'mike');

        $tags = array('tag4', 'tag5', 'tag6');
        $this->_tag->refresh($tags, 2, 'mike');
        
        // other node and handle
        $tags = 'tag9';
        $this->_tag->refresh($tags, 3, 'mike');
        
        $data = array(
            'tag',
            'tag1',
            'tag2',
            'tag3',
            'tag4',
            'tag5',
            'tag6',
            'tag7',
            'tag8',
            'tag9',
        );
        
        // is the row count correct in the tagmap
        $this->assertSame(count(Solar::factory('Abovo_Model_TagMap')->fetchAll()), 11);
    }
    
    public function testFetchAll()
    {
        $tags = array('tag3', 'tag4', 'tag5', 'tag6', 'tag7', 'tag8', 'tag9');
        $this->assertSame($this->_tag->fetchAll(), $tags);
    }
    
    public function testFetchAllByNodeId()
    {
        // fetch by node id
        $tags = array('tag3', 'tag7', 'tag8', 'tag9');
        $this->assertSame($this->_tag->fetchAll(1), $tags);
    }

    public function testFetchAllByHandle()
    {
        // fetch all for user handle
        $tags = array('tag3', 'tag4', 'tag5', 'tag6');
        $this->assertSame($this->_tag->fetchAll(null, 'antti'), $tags);
    }
    
    public function testFetchAllByNodeIdAndHandle()
    {
        // fetch all for node id *and* handle
        $tags = array('tag3');
        $this->assertSame($this->_tag->fetchAll(1, 'antti'), $tags);
    }
    
    public function testFetchAllCounts()
    {
        $counts = array(
            'tag3' => '1',
            'tag4' => '2',
            'tag5' => '2',
            'tag6' => '2',
            'tag7' => '1',
            'tag8' => '1',
            'tag9' => '2',
        );
        
        $this->assertSame($this->_tag->fetchAllCounts(), $counts);
    }

    public function testFetchAllCountsByNodeId()
    {
        $counts = array(
            'tag3' => '1',
            'tag7' => '1',
            'tag8' => '1',
            'tag9' => '1',
        );
        
        $this->assertSame($this->_tag->fetchAllCounts(1), $counts);
        
    }
    
    public function testFetchAllCountsByHandle()
    {
        $this->skip();
        $this->assertSame($this->_tag->fetchAllCounts(null, 'antti'), $counts);
    }
    
    public function testUnicode()
    {
        $tags = array('anttiä');
        $this->_tag->refresh($tags, 100, 'joku');
        
        $this->assertSame($this->_tag->fetchAll(100, 'joku'), $tags);
    }
}
